<?php
require "config.php"; 
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	<meta http-equiv="content-type" content="text/html" />
	<meta name="author" content="thedaigiapro@bitvn.org" />

	<title>Danh sách trang get data</title>
</head>

<body>
<table>
<tr>
<td valign="top">
<?php
$id = (isset($_GET['id'])) ? $_GET['id'] : '0';
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
    $act = $_POST['act'];
    $data = $_POST['data'];
    $data['patternPregIntro'] = json_encode(explode('|||', $data['patternPregIntro']));
    $data['patternReplacePost'] = json_encode(explode('|||', $data['patternReplacePost']));
    $data['patternPregReplacePost'] = json_encode(explode('|||', $data['patternPregReplacePost']));
    $data['patternReplaceIntro'] = json_encode(explode('|||', $data['patternReplaceIntro']));
    $data['patternPregContent'] = json_encode(explode('|||', $data['patternPregContent']));
    $data['patternReplaceContent'] = json_encode(explode('|||', $data['patternReplaceContent']));
    if ($act == 'edit') {
        $update = '';
        foreach($data as $key => $val) {
            
            $update .= " {$key}='".addslashes($val)."',";            
        }
        $update = substr($update, 0, strlen($update)-1);
        $sql = "UPDATE option_link SET ".$update." WHERE id ='{$id}'";        
        $rs = mysql_query($sql) or die (mysql_error());
        echo "<script>window.location.href='list-page.php'</script>";
    } elseif($act == 'add' || $act == 'copy'){
        
        $field = '';
        $value = '';
        foreach($data as $key => $val) {
            $field .= " {$key},";
            $value .= " '".addslashes($val)."',";            
        }  
        $field = substr($field, 0, strlen($field)-1);
        $value = substr($value, 0, strlen($value)-1);
        $sql = "INSERT INTO option_link({$field}) VALUES($value)";        
        mysql_query($sql) or die (mysql_error());
        echo "<script>window.location.href='list-page.php'</script>";
        
    }
    
}

    $sql = "SELECT * FROM option_link"; 
    $rs = mysql_query($sql) or die (mysql_error());
     echo '<table align="center" width="100%">';
     echo '<tr>';
        echo '<td>ID</td>';
        echo '<td width="200">Link</td>';        
        echo '<td>Chuyên mục</td>';
        echo '<td>Active</td>';
        echo '<td>Action</td>';
        echo '</tr>';
    while ($row = mysql_fetch_array($rs)) {    
        echo '<tr>';
        echo '<td>'.$row['id'].'</td>';
        echo '<td><a href="list-page.php?action=edit&id='.$row['id'].'">'.substr($row['linkGetData'],0,35).'</a></td>';
        echo '<td>'.$row['catId'].'</td>';
        echo '<td>'.$row['active'].'</td>';
        echo '<td>
        <a href="list-page.php?action=edit&id='.$row['id'].'">Sửa</a>|
        <a href="list-page.php?action=copy&id='.$row['id'].'">Copy</a>|
        <a href="list-page.php?action=delete&id='.$row['id'].'">Xóa</a>|';
        if($row['active'] == 0){
            echo '<a href="list-page.php?action=publish&id='.$row['id'].'">Có hiệu lực</a>';
        } else{
            echo '<a href="list-page.php?action=unpublish&id='.$row['id'].'">Vô hiệu</a>';
        }
        echo '</td>';        
        echo '</tr>';          
    }
    echo '<tr><td><a href="list-page.php?action=add">Thêm mới</a></td><td></td></tr>';
    echo '</table>';
    
$act = (isset($_GET['action'])) ? $_GET['action'] : 'list';

if($act == 'unpublish') {
    $sql = "UPDATE option_link SET active ='0' where id ='{$id}'"; 
    $rs = mysql_query($sql) or die (mysql_error());     
    echo "<script>window.location.href='list-page.php'</script>";
}
if($act == 'publish') {
    $sql = "UPDATE option_link SET active ='1' where id ='{$id}'"; 
    $rs = mysql_query($sql) or die (mysql_error());     
    echo "<script>window.location.href='list-page.php'</script>";
}

if($act == 'delete') {
    $sql = "DELETE FROM option_link  WHERE id ='{$id}'"; 
    $rs = mysql_query($sql) or die (mysql_error());     
    echo "<script>window.location.href='list-page.php'</script>";
}

$sql = "SELECT * FROM option_link where id ='{$id}'"; 
$rs = mysql_query($sql) or die (mysql_error());     

$linkGetData = '';
$catId	= '';
$folderImg = '';
$resource = '';
$nameLink = '';
$patternTopList  = '';
$patternBottomList = '';
$regularLinkList = '';
$regularLinkListPos	= '';
$regularImgList = '';
$regularImgListPos = '';
$patternTopPost = '';
$patternBottomPost = '';
$patternReplacePost	= '';
$patternPregReplacePost = '';
$patternTitle = '';
$patternTitlePos = '';
$patternIntro = '';
$patternIntroPos = '';
$patternPregIntro = '';
$patternReplaceIntr = '';
$patternPregContent = '';
$patternReplaceContent = '';
$patternReplaceIntro = '';
$flagLinkImg = '';
$active = '';
 while ($row = mysql_fetch_array($rs)) {     
        $linkGetData = $row['linkGetData'];
        $catId	= $row['catId'];
        $folderImg = $row['folderImg'];
        $resource = $row['resource'];
        $nameLink = $row['nameLink'];
        $patternTopList  = $row['patternTopList'];;
        $patternBottomList = $row['patternBottomList'];
        $regularLinkList = $row['regularLinkList'];
        $regularLinkListPos	= $row['regularLinkListPos'];
        $regularImgList = $row['regularImgList'];
        $regularImgListPos = $row['regularImgListPos'];
        $patternTopPost = $row['patternTopPost'];
        $patternBottomPost = $row['patternBottomPost'];
        
        $patternReplacePost	= (!empty($row['patternReplacePost']))?implode("|||",json_decode($row['patternReplacePost'])):'';
        $patternPregReplacePost = (!empty($row['patternPregReplacePost']))?implode("|||", json_decode($row['patternPregReplacePost'])):'';
        
        $patternTitle = $row['patternTitle'];
        $patternTitlePos = $row['patternTitlePos'];
        $patternIntro = $row['patternIntro'];
        $patternIntroPos = $row['patternIntroPos'];
        $patternPregIntro = (!empty($row['patternPregIntro']))?implode("|||",json_decode($row['patternPregIntro'])):'';
        $patternReplaceIntro =(!empty($row['patternReplaceIntro']))?implode("|||",json_decode($row['patternReplaceIntro'])):''; 
        $patternPregContent = (!empty($row['patternPregContent']))?implode("|||",json_decode($row['patternPregContent'])):'';  
        $patternReplaceContent = (!empty($row['patternReplaceContent']))?implode("|||",json_decode($row['patternReplaceContent'])):'';   
        $flagLinkImg = $row['flagLinkImg'];
        $active = $row['active'];
 }

?>

</td>
<td width="50%">
<strong>Thông Tin chi tiết</strong>
<form method="post">
<input type="hidden" name="act" value="<?php echo $act?>" />
<table border="1">
<tr>
	<td>Link chính</td>
	<td width="60%"><input value="<?php echo $resource?>" type="text" style="width: 95%;" id="link" name="data[resource]" /></td>
</tr>
<tr>
	<td>Link lấy data</td>
	<td><input type="text" style="width: 95%;" id="link" value="<?php echo $linkGetData?>" name="data[linkGetData]" /></td>
</tr>
<tr>
	<td>Danh mục tin</td>
	<td><input type="text" style="width: 95%;" id="link" value="<?php echo $catId?>" name="data[catId]" /></td>
</tr>
<tr>
	<td>Thư mục lưu hình</td>
	<td><input type="text" style="width: 95%;" id="link" value="<?php echo $folderImg?>" name="data[folderImg]" /></td>
</tr>
<tr>
	<td>Giới hạn trên trang danh sách tin</td>
	<td><textarea  rows="3" style="width: 95%;" name="data[patternTopList]"  id="patternTopList"><?php echo $patternTopList?></textarea></td>
</tr>
<tr>
	<td>Giới hạn cuối trang danh sách tin</td>
	<td><textarea  rows="3" style="width: 95%;" name="data[patternBottomList]"  id="patternBottomList"><?php echo $patternBottomList?></textarea></td>
</tr>
<tr>
	<td>Điều kiện lấy link trang danh sách</td>
	<td><textarea  rows="3" style="width: 95%;" name="data[regularLinkList]"  id="regularLinkList"><?php echo $regularLinkList?></textarea></td>
</tr>
<tr>
	<td>Vị trí link trong điều kiện trên</td>
	<td><input type="text" style="width: 95%;" id="regularLinkListPos" name="data[regularLinkListPos]" value="<?php echo $regularLinkListPos?>" /></td>
</tr>
<tr>
	<td>Điều kiện lấy hình trang danh sách</td>
	<td><textarea  rows="3" style="width: 95%;" name="data[regularImgList]"  id="regularImgList"><?php echo $regularImgList?></textarea></td>
</tr>
<tr>
	<td>Vị trí lấy hình trong điều kiện trên</td>
	<td><input type="text" style="width: 95%;" id="regularImgListPos" name="data[regularImgListPos]" value="<?php echo $regularImgListPos?>" /></td>
</tr>
<tr>
	<td>Giới hạn trên bài viết</td>
	<td><textarea  rows="3" style="width: 95%;" name="data[patternTopPost]"  id="patternTopPost"><?php echo $patternTopPost?></textarea></td>
</tr>
<tr>
	<td>Giới hạn cuối bài viết</td>
	<td><textarea  rows="3" style="width: 95%;" name="data[patternBottomPost]"  id="patternBottomPost"><?php echo $patternBottomPost?></textarea></td>
</tr>
<tr>
	<td>Xóa các ký tự không cần thiết trong bài viết(phân cách bởi dấu |||)</td>
	<td><textarea  rows="3" style="width: 95%;" name="data[patternReplacePost]"  id="patternReplacePost"><?php echo $patternReplacePost?></textarea></td>
</tr>
<tr>
	<td>Điều kiện xóa ký tự không cần thiết trong bài viết(phân cách bởi dấu |||)</td>
	<td><textarea  rows="3" style="width: 95%;" name="data[patternPregReplacePost]"  id="patternPregReplacePost"><?php echo $patternPregReplacePost?></textarea></td>
</tr>
<tr>
	<td>Điều kiện lấy tiêu đề</td>
	<td><textarea  rows="3" style="width: 95%;" name="data[patternTitle]"  id="patternTitle"><?php echo $patternTitle?></textarea></td>
</tr>
	
<tr>
	<td>Vị trí lấy tiêu đề</td>
	<td><input type="text" style="width: 95%;" id="patternTitlePos" name="data[patternTitlePos]" value="<?php echo $patternTitlePos?>" /></td>
</tr>
<tr>
	<td>Điều kiện lấy intro</td>
	<td><textarea  rows="3" style="width: 95%;" name="data[patternIntro]"  id="patternIntro"><?php echo $patternIntro?></textarea></td>
</tr>
<tr>
	<td>Vị trí lấy intro</td>
	<td><input type="text" style="width: 95%;" id="patternIntroPos" name="data[patternIntroPos]" value="<?php echo $patternIntroPos?>" /></td>
</tr>
<tr>
	<td>Điều kiện xóa ký tự không cần thiết trong intro (phân cách bởi dấu |||)</td>
	<td><textarea  rows="3" style="width: 95%;" name="data[patternPregIntro]"  id="patternPregIntro"><?php echo $patternPregIntro?></textarea></td>
</tr>
<tr>
	<td>Các ký tự dư thừa trong intro</td>
	<td><textarea  rows="3" style="width: 95%;" name="data[patternReplaceIntro]"  id="patternReplaceIntro"><?php echo $patternReplaceIntro?></textarea></td>
</tr>
<tr>
	<td>Điều kiện xóa trong content(phân cách bởi dấu |||)</td>
	<td><textarea  rows="3" style="width: 95%;" name="data[patternPregContent]"  id="patternPregContent"><?php echo $patternPregContent?></textarea></td>
</tr>
<tr>
	<td>Các ký tự cần xóa trong content(phân cách bởi dấu |||)</td>
	<td><textarea  rows="3" style="width: 95%;" name="data[patternReplaceContent]"  id="patternReplaceContent"><?php echo $patternReplaceContent?></textarea></td>
</tr>
<tr>
	<td>Đường dẫn hình tương đối</td>
	<td><input type="checkbox" value="1" id="flagLinkImg" <?php echo ($flagLinkImg==1)?'checked="true"':'' ?>  name="data[flagLinkImg]" /></td>
</tr>
<tr>
	<td>Có hiệu lực</td>
	<td><input type="checkbox" value="1" id="active" <?php echo ($active==1)?'checked="true"':'' ?>  name="data[active]" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Luu" /> <a href="list-page.php">Hủy</a></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>